/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hxht.storage.server.util;

//import org.hibernate.annotations.common.util.StringHelper;

import org.hibernate.internal.util.StringHelper;


/**
 *
 * @author who am i
 */
//@Keep
public class NvmpNamingStrategy
        implements org.hibernate.cfg.NamingStrategy {

    private static final long serialVersionUID = 7802302400247000492L;
    public static final NvmpNamingStrategy INSTANCE = new NvmpNamingStrategy();
//IdentifierGenerator t;

    @Override
    public String classToTableName(String className) {
        return addUnderscores(StringHelper.unqualify("nvmp_" + className));
    }

    /**
     * 属性转列名
     * @param propertyName
     * @return
     */
    @Override
    public String propertyToColumnName(String propertyName) {
        return addUnderscores(StringHelper.unqualify("nvmp_" + propertyName));
    }

    @Override
    public String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String propertyName) {
        return classToTableName(ownerEntityTable + "_" + propertyName);
    }

    @Override
    public String columnName(String columnName) {
        return addUnderscores(columnName);
    }

    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {
        return propertyToColumnName(propertyName != null ? propertyName : propertyTableName);
    }

    @Override
    public String joinKeyColumnName(String joinedColumn, String joinedTable) {
        return columnName(joinedColumn);
    }

    @Override
    public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
//        System.out.printf("\nlogicalCollectionColumnName\ncolumnName=%s,propertyName=%s,referencedColumn=%s\n", columnName, propertyName, referencedColumn);
        return StringHelper.isNotEmpty(columnName)
                ? columnName
                : StringHelper.unqualify(propertyName) + "_" + referencedColumn;
//        System.out.println("result=" + rec);
//        return rec;
    }

    @Override
    public String logicalCollectionTableName(String tableName, String ownerEntityTable, String associatedEntityTable, String propertyName) {

        if (tableName != null) {
            return tableName;
        } else {
            return new StringBuffer("nvmp_").append(ownerEntityTable).append("_").append(
                    associatedEntityTable != null
                    ? associatedEntityTable
                    : StringHelper.unqualify(propertyName)).toString();
        }
    }

    @Override
    public String logicalColumnName(String columnName, String propertyName) {
        return StringHelper.isNotEmpty(columnName) ? columnName : propertyToColumnName(propertyName);
    }

    private static String addUnderscores(String name) {
        StringBuilder buf = new StringBuilder(name.replace('.', '_'));
        for (int i = 1; i < buf.length() - 1; i++) {
            if (Character.isLowerCase(buf.charAt(i - 1))
                    && Character.isUpperCase(buf.charAt(i))
                    && Character.isLowerCase(buf.charAt(i + 1))) {
                buf.insert(i++, '_');
            }
        }
        return buf.toString().toLowerCase();
    }

    @Override
    public String tableName(String tableName) {
        return addUnderscores(tableName);
    }
}
